/*
 *Class Name:Metterdata
 *Class Function: 实现数据写到磁盘或从磁盘读出。
*/



#include <QFile>
#include <QDataStream>
#include <QDebug>
#include <QStringList>

#include "meatterdata.h"
#include "common.h"

#define FileName "spendFile.sf"

MeatterData::MeatterData()
{
}

void MeatterData::WriteFile(QStringList strList)
{
    QFile spendFile(FileName);
    if (!spendFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
        qDebug() << spendFile.errorString();
        return;
    }

    QTextStream out(&spendFile);
    out.setCodec("UTF-8");

    foreach (QString str, strList) {
        out << str << "\n";
    }

    spendFile.close();
}

void MeatterData::WriteFile(QString spendTime, double spendMoney, QString spendGoods)
{
    QFile spendFile(FileName);
    if (!spendFile.open(QIODevice::WriteOnly|QIODevice::Append | QIODevice::Text)) {
        qDebug() << spendFile.errorString();
        return;
    }
    /*
    QDataStream out(&spendFile);//使用dataStream，没有readLine的说法，所以就采用长度加数据的方式。进行读取和写入。

    out << spendTime << spendMoney << spendGoods;
    qin64 size = spendFile.size();
    spendFile.seek(0);
    cout << size;
    out << spendTime << spendMoney << spendGoods;


    spendFile.close();*/
    QTextStream out(&spendFile);
    out.setCodec("UTF-8");
    out << spendTime<<","<<spendMoney<<","<<spendGoods <<"\n";
    spendFile.close();
}

QStringList MeatterData::ReadFile()
{
    QFile spendFile(FileName);
    if (!spendFile.open(QIODevice::ReadOnly| QIODevice::Text)){
        qDebug() << spendFile.errorString();
        return QStringList();
    }

    QTextStream in(&spendFile);
    in.setCodec("UTF-8");
    QString line = in.readLine();
    QStringList dataList;
    while (!line.isNull()) {
        dataList.append(line);
        line = in.readLine();
    }

    return dataList;
}
